package appapi.controller.oider;

import appapi.Request.DelModuleTypeReq;
import appapi.Request.ModuleTypeDetailReq;
import appapi.Request.ModuletypeReq;
import appapi.Vo.AddModuletypeVo;
import appapi.Vo.EditModuletypeVo;
import appapi.common.ResponseBean;
import appapi.entity.auth.ModuletypeEntity;
import appapi.service.ModulelistService;
import appapi.service.ModuletypeService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 模块类别
 *
 * @author lc
 * @version 1.0
 * @date 2023-05-17 10:08:27
 */
@RestController
@RequestMapping("/api/moduletype")
public class ModuletypeController {
    private static final Logger logger = LoggerFactory.getLogger(ModuletypeController.class);
    @Autowired
    private ModuletypeService moduletypeService;
    @Autowired
    private ModulelistService modulelistService;
    /**
     * 列表
     */
    @GetMapping("/getModuleTypeList")
    @ApiOperation(value = "模块类别列表")
    public ResponseBean<ModuletypeEntity> list(@Valid ModuletypeReq req) {
        try {
            List<ModuletypeEntity> dataList = moduletypeService.list(req);
            return ResponseBean.success(dataList);
        } catch (Exception e) {
            logger.error("获取模块类别列表异常", e);
            return ResponseBean.fail("获取模块类别列表异常");
        }
    }

    /**
     * 信息
     */
    @GetMapping("/getModuleTypeDetail")
    @ApiOperation(value = "模块类别信息")
    public ResponseBean<ModuletypeEntity> detail(ModuleTypeDetailReq moduleTypeReq) {
        try {
            if (moduleTypeReq.getModuTypeId() == null) {
                return ResponseBean.fail("类别ID不能为空");
            }
            ModuletypeEntity entity = moduletypeService.detail(moduleTypeReq.getModuTypeId());
            if (entity != null) {
                return ResponseBean.success(entity);
            } else {
                return ResponseBean.fail("获取模块类别详情失败");
            }
        } catch (Exception e) {
            logger.error("获取模块类别详情异常", e);
            return ResponseBean.fail("获取模块类别详情异常");
        }
    }

    /**
     * 新增
     */
    @ApiOperation(value = "新增模块类别")
    @PostMapping(value = "/addModuleType")
    public ResponseBean insert(@RequestBody AddModuletypeVo moduleTypeVo) {
        try {
            ModuletypeEntity entity = moduleTypeVo.vo2Entity();
            entity.setCreateTime(LocalDateTime.now());
            entity.setUpdateTime(LocalDateTime.now());
            int count = moduletypeService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("类别名称已存在，不能重复添加");
            }
            int iRet = moduletypeService.insert(entity);
            if (iRet > 0) {
                return ResponseBean.success("保存成功");
            } else {
                return ResponseBean.fail("保存失败");
            }
        } catch (Exception e) {
            logger.error("新增模块类别异常", e);
            return ResponseBean.fail("新增模块类别异常");
        }
    }

    /**
     * 修改
     */
    @ApiOperation(value = "修改模块类别")
    @PostMapping(value = "/editModuleType")
    public ResponseBean update(@RequestBody EditModuletypeVo moduleTypeVo) {
        try {
            ModuletypeEntity entity = moduleTypeVo.vo2Entity();
            ;
            entity.setUpdateTime(LocalDateTime.now());
            int count = moduletypeService.count(entity);
            if (count >= 1) {
                return ResponseBean.fail("类别名称已存在，不能重复修改");
            }
            int iRet = moduletypeService.update(entity);
            if (iRet > 0) {
                return ResponseBean.success("更新成功");
            } else {
                return ResponseBean.fail("更新失败");
            }
        } catch (Exception e) {
            logger.error("更新模块类别异常", e);
            return ResponseBean.fail("更新模块类别异常");
        }
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除模块类别")
    @PostMapping(value = "/deleteModuleType")
    public ResponseBean delete(@RequestBody  DelModuleTypeReq req) {
        try {
            if (req.getModuTypeId() == null) {
                return ResponseBean.fail("模块ID不能为空");
            }
            int iRet = moduletypeService.delete(req.getModuTypeId());
            if (iRet >= -1) {
                return ResponseBean.fail("存在引用不能删除");
            }
            else if (iRet > 0) {
                return ResponseBean.success("删除成功");
            } else {
                return ResponseBean.fail("删除失败");
            }
        } catch (Exception e) {
            logger.error("删除模块类别异常", e);
            return ResponseBean.fail("删除模块类别异常");
        }
    }

}
